# coding:utf8
import configparser
from tkinter import *
from tkinter import filedialog
from tkinter.scrolledtext import ScrolledText
from tkinter.ttk import Notebook

import simplejson as json
from PIL import ImageTk, Image

import tkinter.messagebox
from time import sleep
import os
from login.hi import get_content
from post.postlib import BaiduUser

USER = "1"
PSW = "1"


# 上传图片
def post_image():
    file_name = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("png files","*.png"),("all files","*.*")))
    user = BaiduUser("")
    image_url = user.thread_post_image(file_name)
    html_image_url = "[br][img pic_type=1]%s[/img]" %image_url
    post_content_text.insert(END, html_image_url)


# 发新贴
def post_new():
    user = BaiduUser("")
    user.login()
    tieba_name = tb_name.get()
    tieba_url = "http://tieba.baidu.com/f?kw=%s" % tieba_name
    post_title_value = post_title_content.get()
    post_content_value = post_content_text.get("1.0", 'end-1c')
    post_result = user.post(tieba_url, tieba_name, post_title_value, post_content_value)

    #post_result = json.loads(post_result)
    error_no = post_result['error_no']
    if error_no == '0':
        tkinter.messagebox.showinfo(post_result)
    elif error_no == '40':
        # 需要验证码，用返回的captcha_vcode_str请求验证码
        verify_code_bin = post_result['ob']
        binfile = open(str("verify.jpg"), "wb")
        binfile.write(verify_code_bin)
        binfile.close()


# 回复
def reply():
    user = BaiduUser("")
    user.login()
    tieba_url = "http://tieba.baidu.com/p/5202379497"
    post_content_value = "test"
    user.reply(tieba_url,post_content_value)


# 把cookie存在文件里面
def bind_baidu_cookie():

    binfile = open("./config/post.conf", "w")
    binfile.write("[cookie]\ncookie = "+account_bind_cookie.get("1.0", 'end-1c'))
    binfile.close()
    # cookie写完之后开始请求个人信息
    person_info= get_content("http://tieba.baidu.com/f/user/json_userinfo").text
    user_name = re.findall('"user_name_show":"(.*?)"', person_info)[0]
    root.title = root.title('现在登录的用户是：'+user_name)
    print(user_name)


def show_very_code():
    global USER, PSW
    if USER == "" or PSW == "":
        tkinter.messagebox.showinfo("Error", u"未登录")
        return
    global image_file, im, very_code_label
    try:
        image_file = Image.open("veryCode.png")
    except IOError:
        tkinter.messagebox.showinfo("Error", u"验证码显示失败")
    else:
        # 验证码大小设置
        new_img = image_file.resize((100, 50), Image.BILINEAR)
        im = ImageTk.PhotoImage(new_img)
        very_code_label.configure(image=im)
        very_code_label.pack(side="left")

# global for image display
image_file = im = None

root = Tk()
root.title(u"百度贴吧小工具")
root.geometry("620x600")

# 各个选项卡
tabs = Notebook(root)
tabs.place(x=10, y=10)
account_tab = Frame(tabs, bg='blue', height=500, width=500)
tabs.add(account_tab, text="账号管理")
post_tab = Frame(tabs, bg='red', height=500, width=500)
tabs.add(post_tab, text="发贴管理")
record_tab = Frame(tabs, bg='yellow', height=500, width=500)
tabs.add(record_tab, text="发贴记录")
sign_tab = Frame(tabs, bg='white', height=500, width=500)
tabs.add(sign_tab, text="一键签到")
contact_tab = Frame(tabs, bg='green', height=500, width=500)
tabs.add(contact_tab, text="联系我们")


# 账号管理面板 account_tab

# cookie内容
account_bind_cookie = ScrolledText(account_tab,
                  width=20, height=10,
                  font=('Courier New', 13)
                  )
account_bind_cookie.place(x=10, y=40)

# 先行读取post.conf文件
if os.path.isfile('./config/post.conf'):
    cf = configparser.ConfigParser()
    try:
        cf.read("./config/post.conf")
        cookie = cf.get("cookie", "cookie")
        account_bind_cookie.insert(END, cookie)
    except:
        pass

Label(account_tab, text=u'cookie内容').place(x=10, y=10)
# 绑定验证cookie信息，若正确则返回用户的个人信息
account_bind_btn = Button(account_tab, text=u"绑定", height=1, width=10, command=bind_baidu_cookie)
account_bind_btn.place(x=90, y=10)

# 发贴管理的面板 post_tab

# 贴吧名称
tb_name_label = Label(post_tab, text="贴吧名称")
tb_name_label.place(x=10, y=10)
tb_name = Entry(post_tab)
tb_name.place(x=100, y=10)

post_title_label = Label(post_tab, text="发贴标题")
post_title_label.place(x=10, y=50)
post_title_content = Entry(post_tab)
post_title_content.place(x=100, y=50)

post_content_label = Label(post_tab, text="发贴内容")
post_content_label.place(x=10, y=100)
post_content_text = ScrolledText(post_tab,
                  width=30, height=10,
                  font=('Courier New', 13)
                  )
post_content_text.place(x=100, y=100)

post_new_btn = Button(post_tab, text="发贴", height=1, width=10, command=post_new)
post_new_btn.place(x=300, y=320)
post_image_btn = Button(post_tab, text="添加图片", height=1, width=10, command=post_image)
post_image_btn.place(x=100, y=320)




root.mainloop()


